<?php

require_once('./modules/util.inc');
require_once("./modules/ext/securimage/securimage.php");
require_once("./modules/ext/recaptcha/recaptchalib.php");

class RegisterRenderer extends Renderer
{
	var $message = "";
	
	function pageLoad()
	{
		switch( $this->action )
		{
			case "register":
				$this->doRegister();
				break;
			case "":
			case "show":
			default:
				break;
		}
	}
	
	function isPermitted()
	{
		if( $this->config->get("./features/registration/@enabled") == "1" && !isset($this->user))
			return true;
		else
			return false;
	}
	
	function doRegister()
	{
		$username	= Util::safeGetPost('txtUsername');
		$pwd 		= Util::safeGetPost('txtPassword');
		$screen 	= Util::safeGetPost('txtScreenName');
		$email 		= Util::safeGetPost('txtEmail');
		$site 		= Util::safeGetPost('txtSiteURL');
		$siteTitle 	= Util::safeGetPost('txtSiteTitle');
		
		if(!$this->validateForm())
			return;
		
		if(strlen($screen)== 0)
			$screen = $username;
			
		$user = new User();
		
		$IP = Util::getUserIP();

		$this->message = $user->create($username,1,$pwd,$screen,$email,$site,$siteTitle,$IP);
		
		if(strlen($this->message) == 0)
			header("Location: index.php?pg=register&act=success");			
	}

	function validateForm()
	{		
		$username	= Util::safeGetPost('txtUsername');
		$email 		= Util::safeGetPost('txtEmail');
		$pwd 		= Util::safeGetPost('txtPassword');
		$confPwd	= Util::safeGetPost('txtConfirmPassword');

		if(strlen($username) == 0 ||
			strlen($email) == 0 ||
			strlen($pwd) == 0 )
		{
			$this->message = "Missing mandatory field";
			return false;
		}
		if ($pwd != $confPwd)
		{
			$this->message = "Passwords do not match";
			return false;
		}
		
		if (!Util::isEmail($email))
		{
			$this->message = "Not a valid email address";
			return false;
		}

		$failed = false;
		if($this->config->get('./useRecaptcha') == '1')
		{
			$resp = recaptcha_check_answer ("6LdVzgYAAAAAAAW6-LIzpEry1EdVAJ9OvCuIAsKY",
									$_SERVER["REMOTE_ADDR"],
									$_POST["recaptcha_challenge_field"],
									$_POST["recaptcha_response_field"]);
			
			if (!$resp->is_valid) 
				$failed = true;			
		}
		else
		{
			$captcha = new Securimage();
			if($captcha->check(Util::safeGetPost('txtCaptcha')) == false) 
				$failed = true;
		}
		
		if($failed)
		{
			$this->message = "Please copy image text correctly";
			return false;
		}
		
		return true;
	}
	
	function renderContent()
	{
		global $content;
		global $siteName;
		global $G_HOME;
		
		$content = 
		"<div class='headerPitch'>
			<p><h1>Register to ".$siteName."</h1></p>
		</div>";
		if( $this->action == "success")
		{
			$content .= "<div class='pageContent'><p><label colspan='2' class='message success'>User created successfully</label><p/>
			please <a href='".Util::combinePath($G_HOME,"index.php?pg=login")."'>login here</a></p></div>";
		}
		else
		{
			$content .= "<div class='pageContent'>
			<p>
			Registration process is quick and simple. please enter the details below<br/>
			Upon submiting, you will receive a confirmation email with a link you need to follow, in order to activate your account.<br/>
			</p>
			<p>
			<form method='POST' action='".Util::combinePath($G_HOME,"index.php?pg=register&act=register")."'>
			<fieldset style='width:440px;padding:20px;'>
				<legend>Please provide the following details:</legend>
				<label class=\"fieldTitle mandatory\">username:</label><input class='text' type='text' name='txtUsername' id='txtUsername' size='30' maxlength='20' value='".Util::safeGetPost('txtUsername')."'></input>
				<label class=\"fieldTitle mandatory\">password:</label> <input class='text' type='password' name='txtPassword' id='txtPassword' size='30' maxlength='20'></input>
				<label class=\"fieldTitle mandatory\">confirm password:</label> <input class='text' type='password' name='txtConfirmPassword' id='txtConfirmPassword' size='30' maxlength='20'></input>
				<label class=\"fieldTitle \">screen name:</label> <input class='text' type='text' name='txtScreenName' id='txtScreenName' size='30' maxlength='20' value='".Util::safeGetPost('txtScreenName')."'></input>
				<label class=\"fieldTitle mandatory\">Email:</label> <input class='text' type='text' name='txtEmail' id='txtEmail' size='30' maxlength='1000' value='".Util::safeGetPost('txtEmail')."'></input>
				<label class=\"fieldTitle\">Site URL:</label> <input class='text' type='text' name='txtSiteURL' id='txtSiteURL' size='30' maxlength='1000' value='".Util::safeGetPost('txtSiteURL')."'></input>
				<label class=\"fieldTitle\">Site title:</label> <input class='text' type='text' name='txtSiteTitle' id='txtSiteTitle' size='30' maxlength='1000' value='".Util::safeGetPost('txtSiteTitle')."'></input>";
				if($this->config->get('./useRecaptcha') == '1')
				{
					$content .= recaptcha_get_html("6LdVzgYAAAAAAJo5-GBbXGu-l3OoDmfD8Hx8EojQ");
				}
				else
				{
					$content .= "<label class=\"fieldTitle mandatory\">Image text:</label> <input class='text captcha' type='text' name='txtCaptcha' id='txtCaptcha' size='14' maxlength='10'></input>
					<img class='captcha' src=\"".Util::combinePath($G_HOME,"modules/ext/securimage/securimage_show.php?sid=".md5(uniqid(time())))."\" id=\"image\" align=\"absmiddle\" />";				
				}
				
				$content .= "<input class=\"submit\" type='submit' value='submit'/>";
			if( strlen($this->message) )
				$content .= "<label colspan='2' class='message failure'>$this->message</label>";		
			$content .= "				</fieldset>			
				</form>
				<span class='message comment'>* fields marked in bold are mandatory.</span>
				</p>
				</div>";
		}
	}

	function renderLoginBox()
	{
		global $loginBox;
		
		$loginBox = "<div class='loginLine'>
						<ul>";
		if( isset($this->user) )
		{
			$loginBox .= "<li class='signIn'>".$this->user->screenName."</li>
				<li><a rel='nofollow' href='./?pg=logout'>sign out</a></li>";
		}
		else 
		{
			$loginBox .= "<li class='signIn'><a rel='nofollow' href='./?pg=login'>Sign in..</a></li>";

			if($this->config->get("./features/registration/@enabled") == "1")
			{
					$loginBox .="<li class='register'><a rel='nofollow' href='./?pg=register'>register</a></li>";
			}
		}
		$loginBox .="	</ul>
					</div>";
	}
}

$renderer = new RegisterRenderer();

?>